package com.huluobo.a2107mvpimdemo.contact.view;


import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.huluobo.a2107mvpimdemo.R;
import com.huluobo.a2107mvpimdemo.adapter.MyContactAdapter;
import com.huluobo.a2107mvpimdemo.base.BaseFragment;
import com.huluobo.a2107mvpimdemo.bean.ContactListItemBean;
import com.huluobo.a2107mvpimdemo.contact.presenter.ContactContract;
import com.huluobo.a2107mvpimdemo.contact.presenter.ContactPresenter;
import com.huluobo.a2107mvpimdemo.view.SlideBar;

import java.util.ArrayList;
import java.util.List;

public class MyContactFragment extends BaseFragment<ContactContract.View, ContactPresenter> implements ContactContract.View {

    private SwipeRefreshLayout swipeRefreshLayout;

    private RecyclerView recyclerView;

    private List<ContactListItemBean> mListItemBeans = new ArrayList<>();

    private MyContactAdapter adapter;
    private SlideBar slideBar;

    private TextView section;

    @Override
    protected int bindLayoutId() {
        return R.layout.fragment_my_contact;
    }

    @Override
    protected void initData() {
        presenter = new ContactPresenter();
        presenter.loadContact();
    }

    @Override
    protected void initView() {
        swipeRefreshLayout = findViewById(R.id.contact_swipe);
        recyclerView = findViewById(R.id.contact_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MyContactAdapter(mListItemBeans, getContext());
        recyclerView.setAdapter(adapter);
        slideBar = findViewById(R.id.contact_slideBar);
        section = findViewById(R.id.contact_section);
        slideBar.setOnSectionChangeListener(new SlideBar.OnSectionChangeListener() {
            @Override
            public void onSectionChange(String firstLetter) {
                section.setVisibility(View.VISIBLE);
                section.setText(firstLetter);
                moveToLetterPosition(firstLetter);
            }

            @Override
            public void onSlideFinish() {
                section.setVisibility(View.GONE);
            }
        });

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.loadContact();
            }
        });
    }

    //recyclerView跟随字母条滚动的方法
    private void moveToLetterPosition(String firstLetter) {
        for (int i = 0; i < mListItemBeans.size(); i++) {
            if ((mListItemBeans.get(i).getFirstLetter() + "").equals(firstLetter)) {
                recyclerView.scrollToPosition(i);//如果对比首字符相等,则让recyclerView滚动到对应的下标位置
            }
        }
    }

    @Override
    public void onLoadContactSuccess(List<ContactListItemBean> listItemBeans) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
                mListItemBeans.clear();
                mListItemBeans.addAll(listItemBeans);
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onLoadContactFailed(String error) {
        swipeRefreshLayout.setRefreshing(false);
        Log.i("TagA", "error:" + error);
    }
}